package org.zjx.command.identify;

import lombok.Getter;
import org.zjx.command.Command;
import org.zjx.command.basic.ScreenshotCommand;
import org.zjx.command.composite.IdentifyFontCommand;
import org.zjx.command.login.LoginCommandConfig;
import org.zjx.core.IWindow;
import org.zjx.entity.Address;
import org.zjx.entity.ParagraphBean;
import org.zjx.entity.Rect;
import org.zjx.enums.FontAttribute;
import org.zjx.identify.PaneUtil;
import org.zjx.state.StateManager;
import org.zjx.util.ImageUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IdentifyAddressCommand extends Command {
    Rectangle addressSimpleRectangle = LoginCommandConfig.createRect(11, 25, 137, 40);
    Rectangle addressDoubleRectangle = LoginCommandConfig.createRect(11, 23, 137, 54);

    private BufferedImage screenshot;
    @Getter
    private Address address;

    public IdentifyAddressCommand(IWindow targetWindow, StateManager stateManager, BufferedImage screenshot) {
        super(targetWindow, false, stateManager);
        this.screenshot = screenshot;
    }

    @Override
    protected void doExecute() throws Exception {
        if (screenshot == null) {
            ScreenshotCommand screenshotCommand = new ScreenshotCommand(targetWindow);
            screenshotCommand.execute();
            this.screenshot = screenshotCommand.getScreenshot();
        }

        Rectangle rectangle = isAddressSingLine() ? addressSimpleRectangle : addressDoubleRectangle;
        List<Color> colorList = Collections.singletonList(new Color(255, 255, 255));
        IdentifyFontCommand identifyFontCommand = new IdentifyFontCommand(targetWindow, stateManager, screenshot, rectangle, colorList, FontAttribute.LOCATION, 0);
        identifyFontCommand.execute();
        List<ParagraphBean.Word> result = identifyFontCommand.getResult();
        String addressStr = IdentifyFontCommand.identifyString(result);
        this.address = matcherLocation(addressStr);
    }

    public boolean isAddressSingLine() {
        Rect rect1 = addressRect(addressSimpleRectangle);
//        Rect rect2 = addressRect(addressDoubleRectangle);
        return rect1 != null;
    }

    private Rect addressRect(Rectangle rectangle) {
        BufferedImage bufferedImage = ImageUtil.cutImage(screenshot, rectangle);
        PaneUtil paneUtil = new PaneUtil(bufferedImage, 3, 8, Collections.singletonList(new Color(104, 100, 104)));
        List<Rect> rects = paneUtil.detectRect();
        return rects.isEmpty() ? null : rects.get(0);
    }

    public static Address matcherLocation(String location) {
        String regex = "([\\w\\u4e00-\\u9fa5]+)【([0-9]+)，([0-9]+)】";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(location);
        if (matcher.find()) {
            String city = matcher.group(1);
            int x = Integer.parseInt(matcher.group(2));
            int y = Integer.parseInt(matcher.group(3));
            return new Address(city, x, y);
        } else {
            return null;
        }
    }
}
